<?php

namespace app\admin\model\article;


use think\Model;
use think\Db;
use think\Session;
/**
 * 房源模型
 */
class Article extends Model{
    protected $name = 'yonghui_building';

    /**
     * 房源详情
     */
    public static function detail($article_id){
        return self::get($article_id);
    }

    /**
     * 获取房源列表
     */
    public function getList($serach){
        //总平台查看所有//自营查看自己//渠道查看所属自营公司
        $type=Session::get('yoshop_adminuser')['user']['type'];
        if($type==1){
            //平台
            $where['status']=['in',[1,2]];
        }elseif($type==2){
            //自营
            $where['status']=['in',[1,2]];
            $where['companyId']=['=',Session::get('yoshop_adminuser')['user']['company_id']];
        }else{
            //渠道 $where['status']=['=',1];
            $com=Db::name('yonghui_company')->where(['id'=>Session::get('yoshop_adminuser')['user']['company_id']])->find();
            $where['companyId']=['=',$com['upId']];
        }
        if(!empty($serach['search'])){
            $where['name|circle']=['like','%'.$serach['search'].'%'];
        }
        return $this
            ->where($where)
            ->order(['createTime' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);
    }
    /**
     * 新增房源信息
     */
    public function add($data){
        return $this->allowField(true)->save($data);
    }
    /**
     * 更新房源信息
     */
    public function edit($data){
        return $this->allowField(true)->save($data) !== false;
    }
    /**
     * 软删除
     */
    public function setDelete(){
        return $this->allowField(true)->save(['status' => 2]);
    }

    /**
     * 上架
     */
    public function up(){
        return $this->allowField(true)->save(['status' => 1]);
    }
}